
package ploy.compiler;

import java.util.*;

import org.objectweb.asm.*;
import org.objectweb.asm.util.*;
import org.objectweb.asm.commons.*;

import ploy.lang.*;

/**
 * A Var is an AST node representing a Ploy variable.
 */
class Var extends AST {

    private Symbol name;

    /* We memoize the location of this variable. */
    private VarLocation loc;

    public Var( Symbol n ) {
	name = n;
    }

    public String toString( ) {
	return name.toString();
    }

    /**
     * The free variables of a variable is only itself.
     */
    protected TreeSet<Symbol> calculateFreeVars( ) {
	TreeSet<Symbol> s = new TreeSet<Symbol>( );
	s.add( name );
	return s;
    }

    /**
     * To generate bytecode for this variable, we delegate to its location.
     */
    public FrameSize generateBytecode( MethodVisitor mv ) {
        return loc.generateBytecode(mv);
    }

    /**
     * Memoize the location of this variable.
     */
    public void locateVariables( VarLocationTable t ) {
	loc = t.get( name );
    }

}